﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Config;
using Dict.Blo;
using Dict.DataModel;
using ProtoBuf.Message;
using Dict;
namespace UI
{
    public class UIFieldSetHpPage : MonoBehaviour
    {
        public GameObject scrollView;
        public GameObject grid;
        public GameObject hpIcon;

        public GameObject firstHp;
        public GameObject secHp;

        public TextureKit hpIconOne;
        public UILabel hpNameOne;
        public TextureKit hpIconTwo;
        public UILabel hpNameTwo;

        public GameObject leftAddIcon;
        public GameObject rightAddIcon;

        UIFieldHpSetPanel hpPanel;
        List<string> allItemList = new List<string>();
        List<string> allCookList = new List<string>();

        List<string> haveItemList = new List<string>();
        List<string> haveCookList = new List<string>();

        List<string> noItemList = new List<string>();
        List<string> noCookList = new List<string>();
        List<GameObject> allList = new List<GameObject>();
        Dictionary<int, string> hpItems = new Dictionary<int, string>();
        float scale = 0.9f;
        string leftHpName;
        string rightHpName;
        TextureKit curObj;
        UILabel curLabel;
        string curId;
        int curType;
        int LType;
        int RType;
        //public
        // Use this for initialization
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }

        public void show(UIFieldHpSetPanel panel)
        {
            clean();
            this.gameObject.SetActive(true);
            getScrollItems();
            hpPanel = panel;

            showBottomBtn();

            
            
        }
        void getScrollItems()
        {
            var cookingList = DictDataManager.Instance.tableFishSkillDao.GetByOneIndex("dish");
            var itemList = DictDataManager.Instance.tableItemDao.GetByOneIndex(16);
            foreach(var aa in itemList)
            {
                allItemList.Add(aa.itemId);
            }
            foreach (var aa in cookingList)
            {
                if (aa.ValueType == "onetimeRecover")
                {
                    allCookList.Add(aa.FishItemID);
                }
                
            }

            sortAllList();

            setScrollView(haveItemList);
            setCookScrollView(haveCookList);
            setScrollView(noItemList);
            setCookScrollView(noCookList);

        }
        
        void sortAllList()
        {
            //haveItemList
            foreach(var aa in allItemList)
            {
                var num = StaticData.playerData.GetSimpleGoods(aa).num;
                if (num > 0)
                    haveItemList.Add(aa);
                else
                    noItemList.Add(aa);
            }
            foreach (var aa in allCookList)
            {
                var num = StaticData.playerData.FishItems[aa].num;
                if (num > 0)
                    haveCookList.Add(aa);
                else
                    noCookList.Add(aa);
            }
            
        }

        void showBottomBtn()
        {
            bool showLeft = true;
            bool showRight = true;
            hpItems = StaticData.playerData.setHpItems;
            
            leftHpName = "";
            rightHpName = "";
            string str1 = "";
            string str2 = "";
            if (hpItems[0] == "")
            {
                leftAddIcon.gameObject.SetActive(true);
                hpIconOne.gameObject.SetActive(false);
                hpNameOne.gameObject.SetActive(false);
                showLeft = false;
                //str1 = "item_068";
            }
            else
            {
                leftAddIcon.gameObject.SetActive(false);
                hpIconOne.gameObject.SetActive(true);
                hpNameOne.gameObject.SetActive(true);
                str1 = hpItems[0];
            }
            if (hpItems[1] == "")
            {
                rightAddIcon.gameObject.SetActive(true);
                hpIconTwo.gameObject.SetActive(false);
                hpNameTwo.gameObject.SetActive(false);
                showRight = false;
                //str2 = "item_069";
            }
            else
            {
                rightAddIcon.gameObject.SetActive(false);
                hpIconTwo.gameObject.SetActive(true);
                hpNameTwo.gameObject.SetActive(true);
                str2 = hpItems[1];
            }
            if(showLeft)
            {
                string[] base1 = str1.Split('(');
                string[] sArrSub = base1[0].Split('_');
                int isItem = 0;
                if (sArrSub[0] == "FishItem")
                {
                    isItem = 1;
                }
                openSetItemData(base1[0], hpIconOne, hpNameOne, 3, isItem);
                leftHpName = str1;
            }
            if(showRight)
            {
                string[] base2 = str2.Split('(');
                string[] sArrSubT = base2[0].Split('_');
                int isItemT = 0;
                if (sArrSubT[0] == "FishItem")
                {
                    isItemT = 1;
                }
                openSetItemData(base2[0], hpIconTwo, hpNameTwo, 3, isItemT);
                rightHpName = str2;
            }
            
        }
        void setScrollView(List<string> list)
        {
            foreach (var aa in list)
            {
                GameObject obj = Instantiate(hpIcon) as GameObject;
                obj.GetComponent<HpIcon>().setIcon(aa);
                obj.transform.parent = grid.transform;
                obj.transform.name = aa;
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localScale = new Vector3(scale, scale, scale);
                obj.transform.localEulerAngles = Vector3.zero;
                obj.gameObject.SetActive(true);
                allList.Add(obj);
            }
            grid.GetComponent<UIGrid>().Reposition();
            //grid.GetComponent<UIGrid>().repositionNow = true;
            scrollView.GetComponent<UIScrollView>().ResetPosition();
        }
        void setCookScrollView(List<string> list)
        {
            foreach (var aa in list)
            {
                GameObject obj = Instantiate(hpIcon) as GameObject;
                obj.GetComponent<HpIcon>().setFishIcon(aa);
                obj.transform.parent = grid.transform;
                obj.transform.name = aa;
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localScale = new Vector3(scale, scale, scale);
                obj.transform.localEulerAngles = Vector3.zero;
                obj.gameObject.SetActive(true);
                allList.Add(obj);
            }
            scrollView.GetComponent<UIScrollView>().ResetPosition();
            grid.GetComponent<UIGrid>().Reposition();
            grid.GetComponent<UIGrid>().repositionNow = true;
        }

        void openSetItemData(string id, TextureKit obj, UILabel name, int flag, int type)
        {
            if (id == leftHpName || id == rightHpName)
            {
                return;
            }
            curObj = obj;
            curId = id;
            curLabel = name;
            curType = type;
            if (flag == 0)
            {
                leftHpName = id;
                //hpPanel.GetComponent<UIFieldHpSetPanel>().setLBtnData(id, "100");
            }
            if (flag == 1)
            {
                rightHpName = id;
                //hpPanel.GetComponent<UIFieldHpSetPanel>().setRBtnData(id, "50");

            }
            if(type == 0)
            {
                obj.iconType = TextureIcon.ITEM;
                var ti = DictDataManager.Instance.tableItemDao.GetById(id);
                obj.textureName = ti.icon;
                name.text = ti.name;
            }
            if(type == 1)
            {
                obj.iconType = TextureIcon.FISH;
                //var ti = DictDataManager.Instance.tableFishSkillDao.GetById(id);
                obj.textureName = curId;
                name.text = TableTextBlo.GetText("name_" + id);
            }
            

        }
        void setItemData(string id, TextureKit obj, UILabel name,int flag,int type)
        {
            if(id == leftHpName || id == rightHpName)
            {
                return;
            }
            curObj = obj;
            curId = id;
            curLabel = name;
            curType = type;
            if(flag == 0)
            {
                leftHpName = id;
                //hpPanel.GetComponent<UIFieldHpSetPanel>().setLBtnData(id, "100");
                CSSideSetShortCut msg = new CSSideSetShortCut();
                msg.position = 1;
                msg.id = id;
                LType = type;
                NetAPI.SendHttp(OpDefine.CSSideSetShortCut, msg, setLeftCallBack);
            }
            if(flag == 1)
            {
                rightHpName = id;
                //hpPanel.GetComponent<UIFieldHpSetPanel>().setRBtnData(id, "50");
                CSSideSetShortCut msg = new CSSideSetShortCut();
                msg.position = 2;
                msg.id = id;
                RType = type;
                NetAPI.SendHttp(OpDefine.CSSideSetShortCut, msg, setRightCallBack);
            }
            
        }
        void setLeftCallBack(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                if (curType == 0)
                {
                    curObj.iconType = TextureIcon.ITEM;
                    var ti = DictDataManager.Instance.tableItemDao.GetById(curId);
                    curObj.textureName = ti.icon;
                    curLabel.text = ti.name;
                }
                else if(curType == 1)
                {
                    curObj.iconType = TextureIcon.FISH;
                    //var ti = DictDataManager.Instance.tableFishSkillDao.GetById(curId);
                    curObj.textureName = curId;
                    curLabel.text = TableTextBlo.GetText("name_" + curId);
                }


                hpPanel.GetComponent<UIFieldHpSetPanel>().setLBtnData(leftHpName, "100", LType);
                //hpPanel.GetComponent<UIFieldHpSetPanel>().setRBtnData(rightHpName, "50", RType);
                StaticData.playerData.UpdateHpSet(leftHpName, rightHpName);
            }
        }
        void setRightCallBack(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                if (curType == 0)
                {
                    curObj.iconType = TextureIcon.ITEM;
                    var ti = DictDataManager.Instance.tableItemDao.GetById(curId);
                    curObj.textureName = ti.icon;
                    curLabel.text = ti.name;
                }
                else if (curType == 1)
                {
                    curObj.iconType = TextureIcon.FISH;
                    //var ti = DictDataManager.Instance.tableFishSkillDao.GetById(curId);
                    curObj.textureName = curId;
                    curLabel.text = TableTextBlo.GetText("name_" + curId);
                }


                //hpPanel.GetComponent<UIFieldHpSetPanel>().setLBtnData(leftHpName, "100", LType);
                hpPanel.GetComponent<UIFieldHpSetPanel>().setRBtnData(rightHpName, "50", RType);
                StaticData.playerData.UpdateHpSet(leftHpName, rightHpName);
            }
        }
        public void setHpOne(string guid)
        {
            string[] sArr = guid.Split('(');
            if (sArr[0] == leftHpName || sArr[0] == rightHpName)
            {
                return;
            }
            firstHp.transform.name = sArr[0];
            string[] sArrSub = sArr[0].Split('_');
            int isItem = 0;
            if (sArrSub[0] == "FishItem")
            {
                isItem = 1;
            }
            //hpPanel.GetComponent<UIFieldHpSetPanel>().setLBtnData(sArr[0],"10");
            leftAddIcon.gameObject.SetActive(false);
            hpIconOne.gameObject.SetActive(true);
            hpNameOne.gameObject.SetActive(true);

            setItemData(sArr[0],hpIconOne, hpNameOne,0,isItem);
        }

        public void setHpTwo(string guid)
        {
            string[] sArr = guid.Split('(');
            if (sArr[0] == leftHpName || sArr[0] == rightHpName)
            {
                return;
            }
            secHp.transform.name = sArr[0];
            string[] sArrSub = sArr[0].Split('_');
            int isItem = 0;
            if (sArrSub[0] == "FishItem")
            {
                isItem = 1;
            }
            //hpPanel.GetComponent<UIFieldHpSetPanel>().setRBtnData(sArr[0], "20");
            rightAddIcon.gameObject.SetActive(false);
            hpIconTwo.gameObject.SetActive(true);
            hpNameTwo.gameObject.SetActive(true);

            setItemData(sArr[0], hpIconTwo, hpNameTwo,1,isItem);
        }
        public void onClose()
        {
            this.gameObject.SetActive(false);
        }

        void clean()
        {
            foreach(var aa in allList)
            {
                Destroy(aa);
            }
            allList.Clear();
            allItemList.Clear();
            allCookList.Clear();
            haveItemList.Clear();
            haveCookList.Clear();

            noItemList.Clear();
            noCookList.Clear();
        }
    }
}

